package com.gs.disk.modules.headImgAndWallper.service.mobile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gs.disk.modules.headImgAndWallper.dao.mobile.MobileCategoryDao;
import com.gs.disk.modules.headImgAndWallper.entity.CategoryEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MobileCategoryService {

    @Resource
    private MobileCategoryDao mobileCategoryDao;


    /**
     * 查询所有分类列表
     * @return
     */
    public List<CategoryEntity> list() {

        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.select("id,pid,category_name,category_level,sort");
        wrapper.eq("data_status", 1);
        wrapper.eq("category_level", 1);

        wrapper.orderByAsc("sort");
        List<CategoryEntity> categoryEntities = mobileCategoryDao.selectList(wrapper);

        categoryEntities.forEach(e -> {
            QueryWrapper<CategoryEntity> wrapperChild = new QueryWrapper<>();
            wrapperChild.select("id,pid,category_name,category_level");
            wrapperChild.eq("data_status", 1);
            wrapperChild.eq("category_level", 2);
            wrapperChild.eq("pid", e.getId());
            wrapperChild.orderByAsc("sort");

            List<CategoryEntity> categoryChildrenList = mobileCategoryDao.selectList(wrapperChild);
            e.setChildren(categoryChildrenList);
        });


        return categoryEntities;
    }
}
